﻿using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ShowModels
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Document document = null;
        //View view = null;
        public MainWindow(Document document)
        {

            InitializeComponent();
            this.document = document;


            try
            {
                List<object> objects = new List<object> { "Hello", 123, "World" };
                var safeStrings = objects.OfType<string>().ToList();

                FilteredElementCollector elements = new FilteredElementCollector(document);
                //View GetTypeId 不是 null 
                List<View> views = elements.OfClass(typeof(View)).Where(x => x.GetTypeId() != null && (document.GetElement(x.GetTypeId()) is ViewFamilyType)).Cast<View>().ToList();
                cb_views.ItemsSource = views;
                //改变事件就会触发  然后进 cb_views_SelectionChanged
                cb_views.SelectedIndex = 0;



                #region
                // 获取当前视图
                View activeView = document.ActiveView;
                try
                {

                    #region 获取所有的窗户
                    //过滤器
                    FilteredElementCollector collector = new FilteredElementCollector(document);
                    //获取所有族实例
                    collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);
                    //获取所有族实例的元素
                    var lists = collector.ToElements();

                    //测试输出窗户名字
                    var sb = new StringBuilder();

                    // 获取所有族实例的元素
                    var windows = collector.ToElements();
                    foreach (var element in lists)
                    {
                        var window = document.GetElement(element.Id) as FamilyInstance;
                        var winName = element.Name;
                        var winCategoryName = element.Category.Name;
                        var winCategoryId = element.Category.Id;
                        ;
                        //bWindows.Items.Add(winName);
                        lbWindows.Items.Add(element);
                        sb.Append($"\n 窗户的名字：{winName}");
                    }
                    windowsNums.Text = "窗数量："+lists.Count();

                    //TaskDialog.Show("窗口", sb.ToString());

                    #endregion


                    #region 遍历窗户实例，并添加文本注释
                    // 遍历窗户实例，并添加文本注释
                    foreach (var window in windows)
                    {
                        // 获取窗户的名称
                        string winName = window.Name;

                        // 创建文本类型
                        TextNoteType textNoteType = new FilteredElementCollector(document)
                            .OfClass(typeof(TextNoteType))
                            .FirstElement() as TextNoteType;

                        // 创建文本注释的位置
                        Location location = window.Location as Location;
                        //XYZ point = location.Point;

                        // 在视图中创建文本注释
                        using (Transaction trans = new Transaction(document))
                        {
                            trans.Start("Add Text Note");

                            // 添加文本注释到视图中
                            TextNote.Create(document, activeView.Id, new XYZ(0, 0, 0), winName, textNoteType.Id);

                            trans.Commit();
                        }
                    }

                    #endregion

                    #region 获取所有的门

                    FilteredElementCollector collectorDoor = new FilteredElementCollector(document);
                    var classFilter = new ElementClassFilter(typeof(FamilyInstance));
                    var categoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);

                    //var logicalFilter = new LogicalOrFilter(new List<ElementFilter> { classFilter, categoryFilter });
                    var logicalFilter = new LogicalOrFilter(classFilter, categoryFilter );
                    collectorDoor.WherePasses(logicalFilter);
                    var listDoor = collectorDoor.ToElements();


                    foreach (var door in listDoor)
                    {
                        lbDoors.Items.Add(door);
                    }


                    doorsNums.Text = "🚪的数量："+listDoor.Count;
                    #endregion




                }
                catch (Exception ex)
                {

                    TaskDialog.Show("异常提示", ex.Message);
                }





                #endregion






            }

            catch (Exception ex)
            {

                TaskDialog.Show("异常提示", ex.Message);
            }


        }


        private void cb_views_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                View view = cb_views.SelectedItem as View;
                if (view != null)
                {
                    LoadView(view);

                    // 更新视图信息  
                    ElementType et = document.GetElement(view.GetTypeId()) as ElementType;
                    if (et != null)
                    {
                        txtViewType.Text = et.Name; // 假设ElementType的名称代表视图类型  
                                                    // 注意：创建日期可能不是直接存储在ElementType或View上的，这里只是示例  
                                                    // 你可能需要其他方式来获取创建日期，或者这个信息可能不直接可用  
                                                    //txtCreationDate.Text = et.CreatedDate.ToShortDateString(); // 假设有CreatedDate属性  
                        txtCreationDate.Text = DateTime.Now.ToShortDateString(); // 假设有CreatedDate属性
                    }
                    else
                    {
                        txtViewType.Text = "未知类型";
                        txtCreationDate.Text = "未知日期";
                    }
                }

            }
            catch (Exception ex)
            {

                TaskDialog.Show("异常提示", ex.Message);
            }
        }



        PreviewControl previewControl = null;
        /// <summary>
        /// 加载视图
        /// </summary>
        public void LoadView(View view)
        {
            try
            {
                if (previewControl != null)
                {
                    //释放
                    this.grid1.Children.Clear();
                    previewControl.Dispose();
                }
                previewControl = new PreviewControl(document, view.Id);
                grid1.Children.Add(previewControl);
            }
            catch (Exception ex)
            {

                TaskDialog.Show("异常提示", ex.Message);
            }
        }
        /// <summary>
        /// 视图选择的改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_views_SelectionChanged2(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //改变的时候获取选中的物体   SelectedItem返回Object 是所有类的基类 就可以转成 其他的类（View）
                View view = cb_views.SelectedItem as View;
                if (view != null)
                {
                    LoadView(view);
                }
            }
            catch (Exception ex)
            {

                TaskDialog.Show("异常",ex.Message);
            }
        }

        private void lbWindows_Selected(object sender, RoutedEventArgs e)
        {
            ;
        }

        /// <summary>
        /// 窗户点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbWindows_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            try
            {
                UIDocument uidoc = new UIDocument(document);

                //MessageBox.Show("选中");

                var select = e.AddedItems[0];
                //实例
                var element = select as Element;
                var id = element.Id;
                ICollection<ElementId> ids = new List<ElementId>() { id };
                if (element != null)
                {
                    //高亮

                }
                uidoc.Selection.SetElementIds(ids);
            }
            catch (Exception ex)
            {

                TaskDialog.Show("异常", ex.Message);
            }
        }
        public int MyProperty { get; set; }

        /// <summary>
        /// 选中门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbDoors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                UIDocument uidoc = new UIDocument(document);

                //MessageBox.Show("选中");

                var select = e.AddedItems[0];
                //实例
                var element = select as Element;
                var id = element.Id;
                ICollection<ElementId> ids = new List<ElementId>() { id };
                if (element != null)
                {
                    //高亮

                }
                uidoc.Selection.SetElementIds(ids);
            }
            catch (Exception ex)
            {

                TaskDialog.Show("异常", ex.Message);
            }
        }
    }
}
